package com.zrrd.jt_admin.shop.type.controller;


import com.zrrd.jt_admin.common.entity.GoodsType;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.GoodsTypyVo;
import com.zrrd.jt_admin.shop.type.service.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhangyk
 * @since 2024-07-02
 */
@Api(tags = "商品类型管理-接口文档")
@RestController
@RequestMapping("admin/goodsType")
@ResponseBody
public class GoodsTypeController {
    @Resource
    private GoodsTypeService goodsTypeService;

    @ApiOperation(value = "商品类型列表")
    //http://localhost:80891/admin/goodsType/1
    @GetMapping("{page}")
    public ResponseResult findByPage(@PathVariable Integer page,
                                     @RequestParam(required = false)Integer limit){
        if (limit == null){
            limit = 5;
        }
        GoodsTypyVo goodsTypyVo = goodsTypeService.findByPage(page,limit);
        return ResponseResult.queryResponse(goodsTypyVo);
    }

    @ApiOperation(value = "增加商品类型")
    @PostMapping
    public ResponseResult addGoodsType(@RequestBody GoodsType goodsType){
        if (goodsType == null){
            return ResponseResult.addResponse(false);
        }
        return ResponseResult.addResponse(goodsTypeService.save(goodsType));
    }

    @ApiOperation(value = "修改商品类型")
    @PutMapping
    public ResponseResult updateGoodsType(@RequestBody GoodsType goodsType){
        if (goodsType == null){
            return ResponseResult.updateResponse(false);
        }
        return ResponseResult.updateResponse(goodsTypeService.updateById(goodsType));
    }

    @ApiOperation(value = "修改商品类型状态")
    @PutMapping("{id}/updateStatus")
    public ResponseResult updateStatusGoodsType(@PathVariable Integer id,Integer status){
        Boolean bStatus=status==1?true:false;
        boolean flag = goodsTypeService.lambdaUpdate()
                .eq(GoodsType::getId, id)
                .set(GoodsType::getStatus, status)
                .update();
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "删除商品类型")
    @DeleteMapping("{id}/delete")
    public ResponseResult deleteGoodsType(@PathVariable Integer id){
        return ResponseResult.removeResponse(goodsTypeService.removeById(id));
    }

    @ApiOperation(value = "批量删除商品类型")
    @DeleteMapping("deleteAll")
    public ResponseResult deleteAllGoodsType(@RequestBody List<Integer> ids){
        return ResponseResult.removeResponse(goodsTypeService.removeByIds(ids));
    }


}

